#include "sourcemodel.h"
#include "newspaper/newspapermanager.h"
#include "QDebug"

SourceListModel::SourceListModel(QObject *parent)
    : QAbstractListModel(parent), m_listSourceInfo()
{

}

void SourceListModel::setSourceInfo(const QList<SourceInfo> &listSourceInfo)
{
    beginResetModel();
    m_listSourceInfo = listSourceInfo;
    endResetModel();
}

void SourceListModel::addSourceInfo(const SourceInfo& aSourceInfo)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_listSourceInfo.append(aSourceInfo);
    endInsertRows();
}

int SourceListModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_listSourceInfo.count();
}

int SourceListModel::count()
{
    return m_listSourceInfo.count();
}

QVariant SourceListModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_listSourceInfo.count())
        return QVariant();

    SourceInfo aSourceInfo = m_listSourceInfo[index.row()];
    if (role == NameRole)
        return aSourceInfo.name();
    else if (role == ImageRole)
    {/*
        QString icon(aSourceInfo.urlImageDomain().toString());
        icon.append("/");
        icon.append(NewspaperManager::instance()->dpiScreen());
        icon.append("/");
        icon.append(aSourceInfo.urlLink().toString());
        icon.append(".png");
        return icon;*/
        //QString linkImage = QString("<img src=\"%1\" />").arg(icon);

        //return linkImage;
        return aSourceInfo.getUpdateFullNameImage();
    }
    else if(role == IdRole)
        return aSourceInfo.id();
    return QVariant();

}


QHash<int, QByteArray> SourceListModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[IdRole] ="idMenu";
    roles[NameRole] = "name";
    roles[ImageRole] = "sourceIcon";
    return roles;
}
